#pragma once

#include <iostream>
using namespace std;

namespace bit
{
	class string
	{
	public:

		typedef char* iterator;
		typedef const char* const_iterator;
		iterator begin()
		{
			return _str;
		}
		iterator end()
		{
			return _str + _size;
		}

		const_iterator begin() const
		{
			return _str;
		}
		const_iterator end() const
		{
			return _str + _size;
		}

		string(const char* str = "")
		{
			_size = _capacity = strlen(str);
			_str = new char[_size + 1];
			strcpy(_str, str);
		}
		string(const string& s)
		{
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}
		string& operator=(string s)
		{
			if (this != &s)
			{
				char* tmp = new char[s._capacity + 1];
				strcpy(tmp, s._str);
				delete[] _str;

				_str = tmp;
				_size = s._size;
				_capacity = s._capacity;
			}

			return *this;
		}
		~string()
		{
			delete[] _str;
			_str = nullptr;
			_size = _capacity = 0;
		}

		const char* c_str() const
		{
			return _str;
		}

		char& operator[](size_t pos)
		{
			return _str[pos];
		}

		const char& operator[](size_t pos) const
		{
			return _str[pos];
		}

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* tmp = new char[n + 1];
				strcpy(tmp, _str);

				delete[] _str;
				_str = tmp;
				_capacity = n;
			}
		}
		void push_back(char c)
		{
			if (_size == _capacity)
			{
				size_t newcapcity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapcity);
			}

			_str[_size] = c;
			_str[_size + 1] = '\0';
			++_size;
		}

		void pop_back()
		{
			--_size;
		}

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_capacity < len + _size)
			{
				reserve(len + _size);
			}

			strcpy(_str + _size,str);

			_size += len;
		}

		string& operator+=(char ch)
		{
			push_back(ch);

			return *this;
		}
		string& operator+=(const char* str)
		{
			append(str);

			return *this;
		}
		
		void insert(size_t pos,char ch)
		{
			if (_size == _capacity)
			{
				size_t newcapcity = _capacity == 0 ? 4 : _capacity * 2;
				reserve(newcapcity);
			}

			size_t end = _size + 1;
			while (end > pos)
			{
				_str[end] = _str[end - 1];
				--end;
			}

			_str[pos] = ch;
			++_size;
		}

		void insert(size_t pos, const char* str)
		{
			size_t len = strlen(str);
			if (_capacity < len + _size)
			{
				reserve(len + _size);
			}

			size_t end = _size + len;
			while (end > pos + len - 1)
			{
				_str[end] = _str[end - len];
				--end;
			}

			memcpy(_str, str, len);

			_size += len;
		}

		void erase(size_t pos,size_t len = npos)
		{
			if (len > _size - pos)
			{
				_str[pos] = '\0';
				_size = pos;
			}
			else
			{
				strcpy(_str, _str + pos  + len);
				_size -= len;
			}
		}
		
		size_t find(char ch,size_t pos)
		{
			for (size_t i = pos; i < _size; ++i)
			{
				if (ch == _str[i])
				{
					return i;
				}
			}

			return npos;
		}

		size_t find(const char* sub, size_t pos)
		{
			char* p = strstr(_str + pos, sub);

			return p - _str;
		}

		void swap(string& s)
		{
			std::swap(_str, s._str);
			std::swap(_size, s._size);
			std::swap(_capacity, s._capacity);
		}

		string substr(size_t pos,size_t len)
		{
			if (len > _size - len)
			{
				string sub(_str + pos);
				return sub;
			}
			else
			{
				string sub;
				for (size_t i = pos; i < len; ++i)
				{
					sub += _str[i];
				}
				return sub;
			}
		}
		
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
		static const size_t npos;
	};
	
	const size_t npos = -1;
	void test_01()
	{
		string s2("hello world");
		string s1;
		s1 = s2;
		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			cout << *it << " ";
			++it;
		}

		cout << endl;
	}
}